//
//  ItemsViewController.m
//  Homepwner
//
//  Created by jiayi xiao on 12-4-20.
//  Copyright 2012年 nju. All rights reserved.
//

#import "ItemsViewController.h"
#import "Possession.h"
#import "PossesionStore.h"
#import "HomepwnerItemCell.h"

@implementation ItemsViewController

- (id)init
{
    self=[super initWithStyle:UITableViewStyleGrouped];
    
    if(self)
    {
        UIBarButtonItem *bbi=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewPossession:)];
        [[self navigationItem] setRightBarButtonItem:bbi];
        [bbi release];
        [[self navigationItem]setTitle:@"Homepwner"];
        [[self navigationItem]setLeftBarButtonItem:[self editButtonItem]];
    }
    
    return self;
}

-(id) initWithStyle:(UITableViewStyle) style
{
    return [self init];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[self tableView]reloadData];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[PossesionStore defaultStore] allPossesions]count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomepwnerItemCell *cell=(HomepwnerItemCell *)[tableView dequeueReusableCellWithIdentifier:@"HomepwnerItemCell"];
    if(!cell)
    {
        cell=[[[HomepwnerItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomepwnerItemCell"] autorelease];
    }
    NSArray *possessions=[[PossesionStore defaultStore]allPossesions];
    Possession *p=[possessions objectAtIndex:[indexPath row]];
    [cell setPossession:p]; 
    return cell;
}

-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        PossesionStore *ps=[PossesionStore defaultStore];
        NSArray *possessions=[ps allPossesions];
        Possession *p=[possessions objectAtIndex:[indexPath row]];
        [ps removePossession:p];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }
}

-(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [[PossesionStore defaultStore] movePossessionAtIndex:[sourceIndexPath row] toIndex:[destinationIndexPath row]];
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ItemDetailViewController *detailViewController=[[[ItemDetailViewController alloc]initForNewItem:NO]autorelease];
    
    NSArray *possessions=[[PossesionStore defaultStore]allPossesions];
    [detailViewController setPossession:[possessions objectAtIndex:[indexPath row]]];
    
    [[self navigationController]pushViewController:detailViewController animated:YES];
}

-(void) toggleEditingMode:(id)sender
{
    if( [self isEditing])
    {
        [sender setTitle:@"Edit" forState:UIControlStateNormal];
        [self setEditing:NO animated:YES];
    }
    else
    {
        [sender setTitle:@"Done" forState:UIControlStateNormal];
        [self setEditing:YES animated:YES];
    }
}

-(void) addNewPossession:(id)sender
{
    Possession *newPossession=[[PossesionStore defaultStore]createPossesion];
    ItemDetailViewController *detailViewController=[[ItemDetailViewController alloc]initForNewItem:YES];
    [detailViewController setDelegate:self];
    [detailViewController setPossession:newPossession];
    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:detailViewController];
    [detailViewController release];
    
    [navController setModalPresentationStyle:UIModalPresentationFormSheet];
    [navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:navController animated:YES];
    
    [navController release];
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad)
    {
        return YES;
    }
    else
    {
        return (toInterfaceOrientation==UIInterfaceOrientationPortrait);
    }
}

-(void) itemDetailViewControllerWillDismiss:(ItemDetailViewController *)vc
{
    [[self tableView]reloadData];
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"HomepwnerItemCell's accessory indicator was tapped.");
    HomepwnerItemCell *cell = (HomepwnerItemCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell toggleContent];
}

@end
